/*
 * GangwayTile.java
 *
 * Created on 26. Dezember 2006, 22:07
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.map.tile;

import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import com.jme.scene.SharedMesh;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Cylinder;
import java.util.HashMap;
import java.util.Map;

/**
 * Provides the "handles" on both sides of a "gangway".
 * @author Pirx
 */
public class GangwayTile extends AbstractTile {
    
    public enum Type {NS, OW};
    
    private static final float COORDS_H[][] = new float[][]{{1,-0.3f},{1, -0.6f}, {-1,-0.3f},{-1, -0.6f}};
    private static final float COORDS_V[][] = new float[][]{{1,-0.8f},{1, 0.8f}, {-1,-0.8f},{-1, 0.8f}};
    
    private final static Matrix3f X_ROT_MATRIX = new Matrix3f();
    private final static Matrix3f Y_ROT_MATRIX = new Matrix3f();
    
    static {
        X_ROT_MATRIX.fromAngleAxis(FastMath.PI/2, new Vector3f(1,0,0));
        Y_ROT_MATRIX.fromAngleAxis(FastMath.PI/2, new Vector3f(0,1,0));
    }

    
    private static final String TYPE = "type";
    private static final String RENDERSTATE = "renderstate";
    
    private Type type;
    private String renderstate;
    private TriMesh[] meshes = new TriMesh[8];
    
    /** Creates a new instance of GangwayTile 
     * @param name 
     * @param ref 
     * @param type direction of gangway
     * @param renderstate 
     */
    public GangwayTile(String name, char ref, Type type, String renderstate) {
        super(name, ref);
        this.type = type;
        this.renderstate = renderstate;
    }
    
    public GangwayTile() {
    }
    
    public void init() {
  
        for (int i = 0; i < 4; i++) {
            meshes[i] = new Cylinder("cyl", 3, 6 , 0.01f, 2, true);
            meshes[i].setLocalTranslation(new Vector3f(COORDS_H[i][0], COORDS_H[i][1], 0));
            if (type == Type.OW) {
                meshes[i].setLocalRotation(Y_ROT_MATRIX);
            }
        }        
         for (int i = 0; i < 4; i++) {
            meshes[i + 4] = new Cylinder("cyl", 3, 6 , 0.01f, 0.7f, true);
            meshes[i + 4].setLocalRotation(X_ROT_MATRIX);
            meshes[i + 4].setLocalTranslation(type == Type.NS 
                    ? X_ROT_MATRIX.mult(new Vector3f(COORDS_V[i][0], 0.65f, COORDS_V[i][1]))
                    : X_ROT_MATRIX.mult(new Vector3f(COORDS_V[i][1], 0.65f, COORDS_V[i][0])));
         }        
    }
    
    public void setParams(Map<String, String> map) {
        type = Type.valueOf(map.get(TYPE));
        renderstate = map.get(RENDERSTATE);
    }

    public Map<String, String> getParams() {
        Map<String, String> map = new HashMap<String, String>();
        map.put(TYPE, type.toString());
        map.put(RENDERSTATE, renderstate);
        return map;
    }

    public TriMesh getMesh(int index) {
        if (meshes[index] == null) {
            init();
        }
        return meshes[index];
    }

    public String getRenderStateName(int index) {
        return renderstate;
    }

    public int getSize() {
        return 8;
    }
}
